package com.foreknow.elmboot.mapper;

import com.foreknow.elmboot.po.DeliveryAddress;
import com.foreknow.elmboot.po.OrderIdCart;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface OrderMapper {

    /**
     * 读取用户地址
     * @param userId 用户ID
     * @return 用户地址集合
     */
    @Select("select * from deliveryaddress where userId=#{userId}")
    public List<DeliveryAddress> readAddress(Integer userId) throws Exception;

    /**
     * 查询新创建合计订单ID
     * @param userId
     * @param businessId
     * @param orderDate
     * @return
     * @throws Exception
     */
    @Select("select orderId from orders where userId=#{userId} and " +
            "businessId=#{businessId} and orderDate=#{orderDate}")
    public Integer readOrdersById(Integer userId, Integer businessId,String orderDate) throws Exception;

    /**
     * 保存用户合计订单
     * @param orderIdCart
     * @return
     * @throws Exception
     */
    @Insert("insert into orders(userId,businessId,orderDate,orderTotal,daId,orderState) " +
            "values(#{userId},#{businessId},#{orderDate},#{orderTotal},#{daId},0)")
    @Options(useGeneratedKeys = true,keyProperty = "orderId",keyColumn = "orderId")
    public int saveOrders(OrderIdCart orderIdCart) throws Exception;

    /**
     * 保存订单明细
     * @param orderId
     * @param foodId
     * @param quantity
     * @return
     * @throws Exception
     */
    @Insert("insert into orderdetailet(orderId,foodId,quantity) values(#{orderId},#{foodId},#{quantity})")
    public int saveOrderDetailet(Integer orderId,Integer foodId,Integer quantity) throws Exception;

}
